#include "speedometerwidget.h"

speedometerwidget::speedometerwidget(QWidget *parent) : BHXJGLBaseWidget(parent)
{
    QSurfaceFormat surfaceFormat(this->format());
    surfaceFormat.setSamples(8);
    setFormat(surfaceFormat);

}
speedometerwidget::~speedometerwidget()
{
    delete speedmeter;
    delete tachometer;

    delete wave;
}

void speedometerwidget::initializeGL()
{
    initializeOpenGLFunctions();

    //glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    shader.Init(this,":/regvertex.vert",":/regfragment.frag");


    camera.SetPosition(glm::vec3(0.0f, 0.0f, 6.0f));
    camera.PitchLimit();

    projection    = glm::mat4(1.0f);
    float width=static_cast<float>(this->width());
    float height=static_cast<float>(this->height());
    height=height>0?height:1;
    projection = glm::perspective(glm::radians(45.0f), width / height, 0.05f, 100.0f);


    speedmeter=new BHXJGLSpeedometer();
    speedmeter->Init(this);


    tachometer=new BHXJGLTachometer();
    tachometer->Init(this);

    wave=new BHXJGLWaterWave();
    wave->Init(this);
}
void speedometerwidget::paintGL()
{
    using namespace glm;

    glEnable(GL_STENCIL_TEST);
    glEnable(GL_DEPTH_TEST);
    //glStencilMask(0xFF);//
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
    //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);


    if(_PaintLine)
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    else
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    BHXJGLPaintParam gpp;


    gpp.setProjection(projection);
    gpp.setView(camera.View(true));

    gpp.setSize(1,1,1);



    //gpp.setRight(vec3(vcos,0,-vsin));
    gpp.setPosition(vec3(-1.5,0,1),true);
    speedmeter->setviewPos(camera.GetPosition());
    speedmeter->setSpeed(speedTemp);
    speedmeter->Paint(gpp);


    //gpp.setRight(vec3(vcos,0,vsin));
    gpp.setPosition(vec3(1.5,0,1),true);
    tachometer->setviewPos(camera.GetPosition());
    tachometer->setRpm(this->rpmTemp);
    tachometer->Paint(gpp);

}


void speedometerwidget::resizeGL(int width, int height)
{
    this->resetProjection(width,height);
}
void speedometerwidget::resetProjection(int width, int height)
{
    projection    = glm::mat4(1.0f);
    height=height>0?height:1;
    //projection = glm::perspective(glm::radians(45.0f), static_cast<float>(width) / static_cast<float>(height), 0.1f, 100.0f);
    // projection = glm::ortho(-(float)width / (float)height, (float)width / (float)height,-1.f, 1.f, 0.1f, 100.0f);
    float top=width>height?1:static_cast<float>(height)/static_cast<float>(width);
    float right=width>height?static_cast<float>(width)/static_cast<float>(height):1;
    if(_ortho)
        projection = glm::ortho(-right,right ,-top, top, 0.1f, 100.0f);
    else
        projection = glm::perspective(glm::radians(45.0f), static_cast<float>(width) / static_cast<float>(height), 0.1f, 100.0f);
}


void speedometerwidget::timerEvent(QTimerEvent *e)
{
    if(keys[Qt::Key_W])
    {
        this->camera.ForWard_Move(0.06f);
        updated=true;
    }
    if(keys[Qt::Key_S])
    {
        this->camera.BackWard_Move(0.06f);
        updated=true;
    }
    if(keys[Qt::Key_A])
    {
        this->camera.LeftWard_Move(0.06f);
        updated=true;
    }
    if(keys[Qt::Key_D])
    {
        this->camera.RightWard_Move(0.06f);
        updated=true;
    }
    if(speedTemp>_Speed)
    {
        speedTemp-=1.5f;
        speedTemp=speedTemp>_Speed?speedTemp:_Speed;
        this->updated=true;
    }
    else if(speedTemp<_Speed)
    {
        speedTemp+=1.5f;
        speedTemp=speedTemp<_Speed?speedTemp:_Speed;
        this->updated=true;
    }

    if(rpmTemp>_RPM)
    {
        rpmTemp-=0.1f;
        rpmTemp=rpmTemp>_RPM?rpmTemp:_RPM;
        this->updated=true;
    }
    else if(rpmTemp<_RPM)
    {
        rpmTemp+=0.1f;
        rpmTemp=rpmTemp<_RPM?rpmTemp:_RPM;
        this->updated=true;
    }


    if(updated)
    {
        updated=false;
        //this->update();
        this->repaint();
    }
}

